﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using MediatR;
using S.Y.Passport.Domain.溯源;
using S.Y.Passport.ErroeCode;
using S.Y.Passport.Infacet;
using S.Y.Passport.Read.API.Applications.Commands;

namespace S.Y.Passport.Read.API.Applications.CommandHandler
{
    public class GoodsDelCommandHandler : IRequestHandler<GoodsDelCommand, SyResult<object>>
    {
        private readonly IBaseRepo<GoodsInfo> goodsInfoRepo;
        private readonly IMapper mapper;

        public GoodsDelCommandHandler(IBaseRepo<GoodsInfo> goodsInfoRepo, IMapper mapper)
        {
            this.goodsInfoRepo = goodsInfoRepo;
            this.mapper = mapper;
        }
        public Task<SyResult<object>> Handle(GoodsDelCommand request, CancellationToken cancellationToken)
        {
            SyResult<object> result = new SyResult<object>();

            result.Msg = "删除成功";
            result.Code = SyCode.删除成功;

            var entity = goodsInfoRepo.GetAll().FirstOrDefault(x => x.GooodId == request.GooodId);

            entity.IsDeleted = true;

            result.Data = entity;

            var res = goodsInfoRepo.Update(entity);
            if (res == 1)
            {
                result.Code = SyCode.删除成功;
                result.Msg = SyCode.删除成功.ToString();
                result.Data = res;
            }
            else
            {
                result.Code = SyCode.删除失败;
                result.Msg = SyCode.删除失败.ToString();
                result.Data = res;
            }

            return Task.FromResult(result); 
        }
    }
}
